<?php
include('FluentExt.php');
include('Str.php');
include('Arr.php');


echo "testing push() and map():";
Arr::fluent()->push('hello', 'world')->map('ucfirst')->join()->out();

echo "\n\n";

echo "testing del(): ";
$arr = array('fruit1' => 'banana', 'fruit2' => 'pineapple', 'fruit3' => 'apple', 'color1' => 'purple');
Arr::fluent($arr)->del("strpos('??', 'fruit') !== false", true)->join()->out();

echo "\n\n";

echo "testing collect(): \n";
$arr = array('Apple', 'Applepie', 'Appletart', 'apples', 'apples', 'orangepie', 'orangeapple', 'purple', 'red', 'applemousse');
Arr::fluent($arr)->collect(
		"(strripos('??', 'apple') === 0 && strtolower('??') != 'apple')"
	)->uniq()->sort()->join()->trim()->out();

echo "\n\n";

echo "testing setIf(): \n";
$arr = array(new Str('banana'), 'banana', 'orange');
Arr::fluent($arr)->setIf("'??' == 'banana'", 'apple' )->out();

echo "\n\n";

echo "testing strApply(): \n";
$arr = array('banana-split', 'apple-pie');
Arr::fluent($arr)->makeStrs()->strApply('split', "'-'")->apply2d('join')->normalStrs()->out();

echo "\n\n";

echo "testing apply() and apply2sub(): \n";
$arr1 = array('orange', 'apple', 'purple');
$arr2 = array('fruit1', 'fruit2', 'color1');
echo "Arr test:\n";
Arr::fluent($arr1)->apply('array_combine', $arr2, '?')->sort()->out();
$str = "apple orange apple guava lime durian";
echo "Str test:\n";
echo Str::fluent($str)->apply('r', 'substr_count', '?', 'apple');
echo "Arr2d apply2sub:\n";
$arr3 = array($arr1, $arr1);
Arr::fluent2d($arr3)->apply2sub('array_combine', $arr2, '?')
	->normal2d()->out();

echo "\n\n";

echo "testing format(): \n";
$str = "There are %d monkeys in a %s in %s";
Str::fluent($str)->format('10', 'tree', 'Thailand')->out();

echo "\n\n";

echo "testing matchPos(): \n";
$str = "applepie applemousse cucumber appletart";
Str::fluent($str)->matchPos('/apple\w+/')->out();

echo "\n\n";

echo "testing match(): \n";
$str = "applepie applemousse cucumber appletart";
Str::fluent($str)->match('/apple\w+/')->sort()->join()->out();

echo "\n\n";

echo "testing matchReplace(): \n";
$str = "applepie applemousse cucumber appletart";
Str::fluent($str)->matchReplace('/(apple)(\w+)/', 'orange${2}')->split()->sort()->join()->out();

echo "\n\n";

echo "testing matchArb(): \n";
$str = "fruit apple fruit orange color purple fruit pineapple color red";
Str::fluent($str)->matchArb('/(fruit)\s(\w+)/', PREG_SET_ORDER, 0, 2)->sort()->join(', ')->out();

echo "\n\n";

echo "testing matchArb(), flatten() and apply2d(): \n";
$str = "fruit apple fruit orange color purple fruit pineapple color red";
echo "1: ";
Str::fluent($str)->matchArb('/(fruit)\s(\w+)/', PREG_SET_ORDER|PREG_OFFSET_CAPTURE, 0, 2)
	->make2d()->at(1)->out();
echo "\n2: ";
Str::fluent($str)->matchArb('/(fruit)\s(\w+)/', PREG_SET_ORDER|PREG_OFFSET_CAPTURE, 0, 2)
	->make2d()->flatten()->join(';')->out();

echo "\n\n";

echo "testing serial() and sort() on 2d array: \n";
$arr = array(
	array('name' => 'John', 'age' => 65), 
	array('name' => 'Mary', 'age' => 32), 
	array('name' => 'Alfred', 'age' => 45));
Arr::fluent2d($arr)->serial()->unserial()->make2d()
	->sort('desc', 'age')->normal2d()->out();

echo "\n\n";

echo "testing map() and flatten(): \n";
$arr = array(
	array('name' => 'John', 'age' => 65), 
	array('name' => 'Mary', 'age' => 32), 
	array('name' => 'Alfred', 'age' => 45));
Arr::fluent2d($arr)->map('strtolower')->flatten('name')->sort()->out();

echo "\n\n";

echo "testing collect() and del() on 2d array: \n";
$arr = array(
	array('name' => 'John', 'age' => 65), 
	array('name' => 'Mary', 'age' => 32), 
	array('name' => 'Jonny', 'age' => 45));
echo "collect(): \n";
Arr::fluent2d($arr)->collect(
			"strripos('??', 'jo') !== false", false, 'name'
		)->out();
echo "del(): \n";
Arr::fluent2d($arr)->del(
		"strripos('??', 'jo') !== false", false, 'name'
	)->out();
		
echo "\n\n";

$arr = array(
	array('id' => '1', 'apples' => 2, 'oranges' => 4),
	array('id' => '1', 'apples' => 7, 'oranges' => 5),
	array('id' => '2', 'apples' => 3, 'oranges' => 9),
	array('id' => '2', 'apples' => 10, 'oranges' => 1)
);
echo "sum() and plot() on 2d array: \n";
echo "sum() with ctrl key: \n";
Arr::fluent2d($arr)->sum('apples,oranges', 'id')->out();
echo "without ctrl key: \n";
Arr::fluent2d($arr)->sum('apples,oranges')->out();
echo "plot() and json(): \n";
echo Arr::fluent2d($arr)->sum('apples,oranges', 'id')->plot()->json();

echo "\n\n";

$arr = array(
	array('id' => '1', 'stamp' => 1199120400, 'apples' => 2, 'oranges' => 4),
	array('id' => '1', 'stamp' => 1199206800, 'apples' => 7, 'oranges' => 5),
	array('id' => '2', 'stamp' => 1199293200, 'apples' => 3, 'oranges' => 9),
	array('id' => '2', 'stamp' => 1199379600, 'apples' => 10, 'oranges' => 1)
);
echo "apply2field(): \n";
echo Arr::fluent2d($arr)->apply2field('date', 'stamp', "d", '?')->sum('apples,oranges', 'stamp')
		->plot(array('Apples','Oranges'))->json();


	
?>